package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.PurchaseOrder;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 请购单
 */
@Component
public class PurchaseOrderDao implements BaseDao<PurchaseOrder> {
    @Override
    public int insert(PurchaseOrder entity) {
        return DBHelper.executeUpdate(
            "insert into purchase_order (ordernumber, price, buycount,is_status, employee_id_apply,otherprice,remark,supplier_id) values (?, ?, ?, ?,?,?,?,?);",
            entity.getOrdernumber(), entity.getPrice(), entity.getBuycount(), entity.getIs_status(),
            entity.getEmployee_id_apply(), entity.getOtherprice(), entity.getRemark(), entity.getSupplier_id());
    }

    @Override
    public int update(PurchaseOrder entity) {
        return DBHelper.executeUpdate(
            "update purchase_order set ordernumber=?, price=?, is_status=?, employee_id_retify=?, employee_id_apply=? where id=?;",
            entity.getOrdernumber(), entity.getPrice(), entity.getIs_status(), entity.getEmployee_id_retify(),
            entity.getEmployee_id_apply(), entity.getId());
    }

    @Override
    public int delete(PurchaseOrder entity) {
        return DBHelper.executeUpdate("update purchase_order set is_deleted=1 where id=?", entity.getId());
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return DBHelper.executeQuery("select * from purchase_order where is_deleted=0 ");
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        PurchaseOrder purchaseorder = (PurchaseOrder)obj[0];
        String sql = "select ro.*,e.realname rname,em.realname aname,su.name suname from purchase_order ro "
            + "left join supplier su on ro.supplier_id=su.id "
            + " left join employee e on ro.employee_id_retify = e.id "
            + "left join employee em on ro.employee_id_apply =em.id " + "where ro.is_deleted = 0  ";
        if (purchaseorder.getIs_status() != -1) {
            sql += " and ro.is_status = " + purchaseorder.getIs_status();
        }
        // else {
        // sql += " and ro.is_status != 9";
        // }
        if (purchaseorder.getOrdernumber() != null && !"".equals(purchaseorder.getOrdernumber())) {
            sql += " and concat(e.realname,em.realname,ordernumber) like '%" + purchaseorder.getOrdernumber() + "%'";
        }
        return DBHelper.executeQuery(sql + " order by ro.gmt_create desc limit ?,?;", page.getBegin(),
            page.getPageSize());
    }

    @Override
    public Map<String, Object> findOne(PurchaseOrder entity) {
        return DBHelper.executeQuery("select * from purchase_order where is_deleted=0 and id=?", entity.getId()).get(0);
    }

    @Override
    public int count(Object... obj) {
        PurchaseOrder purchaseorder = (PurchaseOrder)obj[0];
        String sql =
            "select count(*) counts from purchase_order ro left join employee e on ro.employee_id_retify = e.id "
                + " left join employee em on ro.employee_id_apply =em.id where ro.is_deleted = 0 ";
        if (purchaseorder.getIs_status() != -1) {
            sql += " and ro.is_status = " + purchaseorder.getIs_status();
        }
        // else {
        // sql += " and ro.is_status != 9";
        // }
        if (purchaseorder.getOrdernumber() != null && !"".equals(purchaseorder.getOrdernumber())) {
            sql += " and concat(e.realname,em.realname,ordernumber) like '%" + purchaseorder.getOrdernumber() + "%'";
        }

        return Integer.valueOf(DBHelper.executeQuery(sql).get(0).get("counts").toString());
    }

    public int findMaxId() {
        return Integer
            .valueOf(DBHelper.executeQuery("select max(id) maxid from purchase_order").get(0).get("maxid").toString());
    }

    public int changeStatus(PurchaseOrder purchaseOrder) {
        return DBHelper.executeUpdate("update purchase_order set is_status=?,employee_id_retify=? where id=?",
            purchaseOrder.getIs_status(), purchaseOrder.getEmployee_id_retify(), purchaseOrder.getId());
    }
}
